<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Ad_Services_Banner
{
	public static function validate($data)
	{
		$validator = new Gio_Core_Validator();
		foreach ($data as $index => $value) {
			if (gettype($value) == 'string') {
				$data[$index] = trim($value);
			}
		} 
		$data['messages']['name'] = $validator->setString($data['name'])
												->validate(array('notNull')
																, array(
																	'notNull' => 'banner_name_notnull',
																)
														);
														
		$error = false;
		foreach ($data['messages'] as $index => $message) {
			if ($message) {
				$data['messages_error'] = true;
				return $data;
			}
		}										
		$data['messages_error'] = false;
		return $data; 
	}
	
	public static function add($banner, $items = array())
	{
		$conn		= Gio_Db_Connection::getConnection();
		$bannerDao = new Modules_Ad_Models_Mysql_Banner();
		$bannerDao->setConnection($conn);
		$bannerId = $bannerDao->add($banner);

		if (isset($items['route']) && $items['route']) {
			foreach ($items['route'] as $index => $route) {
				$bannerPageAssoc = array(
					'banner_id'		=> $bannerId,
					'zone_id'		=> $items['zone_id'][$index],
					'route'			=> $route,
					'page_url'		=> $items['url'][$index],
					'page_title'	=> $items['title'][$index]
				);
				Modules_Ad_Services_BannerPageAssoc::add($bannerPageAssoc);
			}
		}
		return $bannerId;
	}
	
	public static function delete($id)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Ad_Models_Mysql_Banner();
		$dao->setConnection($conn);
		return $dao->delete($id);
	}
	
	public static function count($condition = array())
	{
		$conn		= Gio_Db_Connection::getConnection();
		$bannerDao = new Modules_Ad_Models_Mysql_Banner();
		$bannerDao->setConnection($conn);
		return $bannerDao->count($condition);
	}
	
	public static function find($start, $perPage, $condition = array())
	{
		$conn		= Gio_Db_Connection::getConnection();
		$bannerDao = new Modules_Ad_Models_Mysql_Banner();
		$bannerDao->setConnection($conn);
		return $bannerDao->find($start, $perPage, $condition);
	}
	
	public static function getById($bannerId)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$bannerDao = new Modules_Ad_Models_Mysql_Banner();
		$bannerDao->setConnection($conn);
		return $bannerDao->getById($bannerId);
	}
	
	public static function loadBanners()
	{
		$conn = Gio_Db_Connection::getConnection();
		$bannerDao = new Modules_Ad_Models_Mysql_Banner();
		$bannerDao->setConnection($conn);
		return $bannerDao->loadBanners();
	}
	
	public static function getByPage($pageName)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$bannerDao = new Modules_Ad_Models_Mysql_Banner();
		$bannerDao->setConnection($conn);
		return $bannerDao->getByPage($pageName);
	}
	
	public static function update($banner, $items = array())
	{
		$conn		= Gio_Db_Connection::getConnection();
		$bannerDao = new Modules_Ad_Models_Mysql_Banner();
		$bannerDao->setConnection($conn);
		$bannerDao->update($banner);
		
		/**
		 * Delete banner page assoc by banner id
		 */
		Modules_Ad_Services_BannerPageAssoc::deleteByBannerId($banner['banner_id']);
		if (isset($items['route']) && $items['route']) {
			foreach ($items['route'] as $index => $route) {
				$bannerPageAssoc = array(
					'banner_id'		=> $banner['banner_id'],
					'zone_id'		=> $items['zone_id'][$index],
					'route'			=> $route,
					'page_url'		=> $items['url'][$index],
					'page_title'	=> $items['title'][$index]
				);
				Modules_Ad_Services_BannerPageAssoc::add($bannerPageAssoc);
			}
		}
	}
}